package com.xiaodai.miaosha.controller;

import com.xiaodai.miaosha.entity.Goods;
import com.xiaodai.miaosha.entity.SeckillGoods;
import com.xiaodai.miaosha.entity.Msuser;
import com.xiaodai.miaosha.service.IGoodsService;
import com.xiaodai.miaosha.service.IMsuserService;
import com.xiaodai.miaosha.service.ISeckillGoodsService;
import com.xiaodai.miaosha.vo.DetailVo;
import com.xiaodai.miaosha.vo.GoodsVo;
import com.xiaodai.miaosha.vo.RespBean;
import com.xiaodai.miaosha.vo.RespBeanEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * *auther:xiaodai
 * *date:2022/3/1811:44 AM
 * *describe:
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IMsuserService userService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    @RequestMapping(value = "/toSekillList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toSekillList(Model model, @CookieValue("userTicket") String ticket, HttpServletRequest request, HttpServletResponse response){

        Msuser Msuser = userService.getUserByCookie(ticket,request,response);
        if (Msuser == null){
            return "用户未登录！";
        }

//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        String html = (String)valueOperations.get("goodsList");
//        if (!StringUtils.isEmpty(html)){
//            return html;
//        }
        String html = null;
        model.addAttribute("Msuser",Msuser);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList2",webContext);
//        System.out.println(html);
//        if (!StringUtils.isEmpty(html)){
//            valueOperations.set("goodsList",html,1, TimeUnit.MINUTES);
//        }
        return html;
    }

    @RequestMapping(value = "/toDetail/{goodsId}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail(Model model, @CookieValue("userTicket") String ticket, HttpServletRequest request, HttpServletResponse response, @PathVariable Long goodsId){
        if (ticket.isEmpty()){
            return "login";
        }
//        Msuser Msuser = (Msuser)session.getAttribute(ticket);
        Msuser Msuser = userService.getUserByCookie(ticket,request,response);
        if (Msuser == null){
            return "login";
        }
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String)valueOperations.get("goodsDetail:"+goodsId);
        if (!StringUtils.isEmpty(html)){
            return html;
        }
        GoodsVo goods = goodsService.findGoodsVoById(goodsId);
        Date start = goods.getStartDate();
        Date end = goods.getEndDate();
        Date now = new Date();
        int seckillStatus = 0;
        //最好放前端做
        int remainSeconds = 0;
        if (now.before(start)){
            remainSeconds = (int)((start.getTime()-now.getTime())/1000);
        }else if (now.after(end)){
            seckillStatus = 2;
            remainSeconds = 0;
        }else {
            seckillStatus = 1;
            remainSeconds = -1;
        }
        model.addAttribute("Msuser",Msuser);
        model.addAttribute("goods",goods);
        model.addAttribute("seckillStatus",seckillStatus);
        model.addAttribute("remainSeconds",remainSeconds);

        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsDetail",webContext);
        if (!StringUtils.isEmpty(html)){
            valueOperations.set("goodsDetail:"+goodsId,html,60,TimeUnit.SECONDS);
        }
        return html;
    }

    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public RespBean detail(@CookieValue("userTicket") String ticket, HttpServletRequest request, HttpServletResponse response, @PathVariable Long goodsId){

        Msuser Msuser = userService.getUserByCookie(ticket,request,response);
        if (Msuser == null){
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
        GoodsVo goods = goodsService.findGoodsVoById(goodsId);
        Date start = goods.getStartDate();
        Date end = goods.getEndDate();
        Date now = new Date();
        int seckillStatus = 0;
        //最好放前端做
        int remainSeconds = 0;
        if (now.before(start)){
            remainSeconds = (int)((start.getTime()-now.getTime())/1000);
        }else if (now.after(end)){
            seckillStatus = 2;
            remainSeconds = -1;
        }else {
            seckillStatus = 1;
            remainSeconds = 0;
        }
        DetailVo detailVO = new DetailVo();
        detailVO.setGoodsVo(goods);
        detailVO.setMsuser(Msuser);
        detailVO.setRemainSeconds(remainSeconds);
        detailVO.setSecKillStatus(seckillStatus);
//        System.out.println(goods.getSeckillPrice());
        System.out.println(Msuser.getId());
        return RespBean.success(detailVO);
    }

}
